<?php
session_start();
require_once( 'student_db.php' );
 
$m_db = new M_Student();
$user_id = $_SESSION['userid'];
$Year =$m_db->getCurrentYear();
if (isset($_POST['m'])  &&  $_POST['m']!=""){

    if ($_POST['m']== 'load'){
	  
	  $cid = $_POST['cid'];
	  $r = $_POST['r'];
	  $s = $_POST['s'];
	  $n = $_POST['n'];
	  $co = $_POST['co'];
	  $y =(isset($_POST['year'])?$_POST['year']:$m_db->getCurrentYear());
      $result=$m_db->loadStudent($co,$n,$cid,$r,$s,$y);
      $rows=$m_db->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           $data = array();
			$today = date("Y-m-d");	
			$year = date("Y");
			$room_data="";
			$k=1;
            while($row = $result->FetchRow()){
			if ($k==1){
			   $room_data = $m_db->load_room($row['class_id'] + 1);
			  } 
               $data[] = array(0,$row['student_id'],$row['class_id'],$row['code'], $row['title_name'] ." ". $row['thai_name'] . " " .$row['thai_surname']  
			   ,$row['thai_nickname'],$row['class_name'].' / '.$row['room'],$room_data,$row['room']) ;
			   
			   $k++;
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
     
    }else if ($_POST['m']=='room'){ 
		$id=  $_POST['cid'];
		echo json_encode( $m_db->load_room($id));
	}else if ($_POST['m']=='getRoom_amt'){
		$id=  $_POST['cid'];
		$result = $m_db->load_current_amt_room($id);
		$html="";
		while($row = $result->FetchRow()){
			$html.= "<span class='help-inline' style='font-weight:bold;'>ห้อง  ".$row['room']."&nbsp;:&nbsp;&nbsp;</span>";
			$color="";
			if ($row['cnt']<$row['amount']){
				if ($row['cnt'] > ($row['amount'] - 2)){
					$color = 'label-important'; //Nearly full
				}else{
					$color = 'label-success';
				}
			}else{
				$color = '';
			}
			$html.="<span class='label ".$color."' >".$row['cnt']."/".$row['amount']."</span> ";
		}
		 
		echo  $html;
	 
    }else if ($_POST['m']=='add'){
        $ar = array();
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;
		$list = $_POST['list'];
		foreach($list as $val){
			$ar['student_id'] = $val['sid'];
			$ar['class_id']=$val['cid'];
			$ar['room']=$val['r'];
			$ar['year']=$Year + 1;//  $Year;
			
			$m_db->student_AdjustLevel($ar);
		}
		echo 1;
		
    }  
}else{
	if (isset($_GET['m']) && $_GET['m'] !="" ){
		if ($_GET['m'] == 'class'){
			echo json_encode( $m_db->getClassLevel());
		}else if ($_GET['m']=='room'){
			echo  json_encode( $m_db->load_all_room($_GET['i']));
		}else if ($_GET['m'] =='year'){
		  echo json_encode($m_db->getYear_Term());
		}else if ($_GET['m'] =='curryear'){
			echo $m_db->getCurrentYear();
		}
	} 
   
}

function DATEDIFF($date1, $date2){
	
}
 
?>